home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 4.iso
/
public
/
bit
/
src
/
ulib
/
readint.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-08-01
|
3KB
|
167 lines
/***********************************************************************
* $Id: readint.c,v 0.80 1994/02/24 09:48:11 zhao Exp $
*
*. Copyright(c) 1993,1994 by T.C. Zhao
* All rights reserved.
*.
*
* Read an integer (decimal or hex) from a file.
* Valid sepertors are SPACE, \t \n and ,
* Comment is introduced by #
*
* For hex integers, a-f, A-F is assumed continous
***********************************************************************/
#if !defined(lint) && defined(F_ID)
char *id_rdint = "$Id: readint.c,v 0.80 1994/02/24 09:48:11 zhao Exp $";
#endif
#include <stdio.h>
#include <ctype.h> /* for isdigit */
#include "ulib.h"
#define IS_FS(c) ((c)==' ' || (c)=='\t' || (c)=='\n' || (c)==',' )
#define IS_COMMENT(c) ((c) == '#')
static int yell;
void
set_read_silent(int y)
{
yell = !y;
}
void
bad_character(int c)
{
if (yell && c != EOF)
(void) fprintf(stderr, "Bad character %c Code=%d\n", c, c);
}
int
skip_comment(FILE * fp)
{
register int c;
while ((c = getc(fp)) != EOF && c != '\n')
;
return (c != EOF) ? getc(fp) : EOF;
}
/* read an integer [+-]nnn. No way to return error status */
int
readint(FILE * fp)
{
register int c, num = 0, sign = 1;
do
{
c = getc(fp);
while (IS_COMMENT(c))
c = skip_comment(fp);
}
while (IS_FS(c));
if (c == '-' || c == '+')
{
sign = (c == '-') ? -1 : 1;
c = getc(fp);
}
while (isdigit(c))
{
num = 10 * num + c - '0';
c = getc(fp);
}
if (!IS_FS(c))
{
bad_character(c);
num = 123456;
}
return sign * num;
}
/* Read a positive integer. return EOF if error */
int
readpint(FILE * fp)
{
register int c, num = 0;
do
{
c = getc(fp);
while (IS_COMMENT(c))
c = skip_comment(fp);
}
while (IS_FS(c));
if (!(c == '+' || isdigit(c)))
{
bad_character(c);
return EOF;
}
do
{
num = 10 * num + c - '0';
c = getc(fp);
}
while (isdigit(c));
return num;
}
/*
* Read a hex integer
*/
int
readhexint(FILE * fp)
{
register int num = 0, i, c;
static short hextab[256];
/* initialize the hex table */
if (!hextab['1'])
{
for (i = '1'; i <= '9'; i++)
hextab[i] = i - '0';
for (i = 'A'; i <= 'F'; i++)
hextab[i] = 10 + i - 'A';
for (i = 'a'; i <= 'f'; i++)
hextab[i] = 10 + i - 'a';
}
do
{
c = getc(fp);
while (IS_COMMENT(c))
c = skip_comment(fp);
}
while (IS_FS(c));
/*
* demand 0[xX]
*/
if (c != '0' || ((c = getc(fp)) != 'x' && c != 'X'))
{
bad_character(c);
return EOF;
}
/*
* now do the coversion
*/
while ((c = getc(fp)), isxdigit(c))
num = (num << 4) + hextab[c];
return num;
}
#ifdef TEST
#include <stdio.h>
#include <string.h>
main()
{
int p;
while ((p = readint(stdin)) != -1)
fprintf(stderr, "Read: %d\n", p);
}
#endif